Two Excel workarounds for automatic refresh and recalculation

您所在的位置:网站首页 how to automatically refresh a page in chrome Two Excel workarounds for automatic refresh and recalculation

Two Excel workarounds for automatic refresh and recalculation

2023-03-09 00:08| 来源: 网络整理| 查看: 265

There are two ways to make Excel automatically refresh data connections and recalculate a worksheet.  These tricks are important with the Stock and Currency data type in Excel 365 but also other situations.

Normally Excel will update itself when you change a cell value.  These days there are situations where cells change value but Excel does NOT update the worksheet.  In other words, modern Excel has changed in ways that Microsoft hasn’t yet fully adapted to.

Ideally, Excel would have an overall setting to refresh the worksheet every ‘n’ seconds or minutes.  In 2022, Microsoft finally added some Stock/Currency auto-refresh options but they are incomplete and limited.

This article has TWO different ways to force updates in Excel. One is a ‘traditional’ macro approach, the other is a newer trick using PowerQuery in conjunction with single macro.

Either option gives you more control over updating than Microsoft’s simplistic ‘tick the box’ addition to Excel 365.

Stock Data Type

With the Stock Data Type  the ability to update automatically is more important.  Users will want their worksheets to grab the latest prices automatically, something the current preview releases can’t do.  Instead of having a nice automatic ‘ticker’, we’re expected to click ‘Refresh’ to get the latest prices.

The Stock, Currency and other linked data types are curious beasts. They are data connections to external sources but do NOT appear as Excel Data Connections.  That means you can’t setup an automatic data refresh, as you would with normal data connections.  In fact, there’s no exposed controls for the Stock, Currency or other linked data types.

NOW() and other volatile functions

The NOW() function updates to the latest date and time whenever Excel recalculates the worksheet. But if there’s nothing to make that happen, Now() doesn’t change value.  Some external factor is needed to make Excel update Now() and the rest of the worksheet.  In other words, you should be able to glance at a worksheet and know it’s up to the second but that’s not possible with Excel ‘out of the box’.

Microsoft calls NOW() and similar functions ‘volatile’ because their values can change even if no other cells have changed.  Other volatile functions are Today(), Randbetween(), Offset() and Indirect().  In some situations Info(), Cell() and SumIf() can also be volatile.

VBA custom functions can also be tagged as volatile using this line in the function code:

Application.Volatile

That line makes the function run anytime Excel updates/recalcs the worksheet.

The trick is to make an Excel macro which forces data connections to update then mark that function as Volatile so it will be run whenever the workbook is updating.

Extra caution

Maybe you want your worksheet to update automatically as a precaution?  Many old Excel hands remember situations where Excel hasn’t properly updated so they like the ‘belt and braces’ approach (at least occasionally).

Automatic update code

The standard method of forcing automatic update of Excel is a short snippet of VBA code. Here’s what we use, there are many variations on the same theme. The full code is at the bottom of the article.

There are three functions.

RefreshAllDataConn

does the actual refreshing of data connections ( Workbooks(ThisWorkbook.Name).RefreshAll ) and we added two, optional,  lines to display the last time refreshed on the bottom status bar.

If you wanted to be extra careful, add line to explicitly force recalculation.  Either  ActiveSheet.Calculate  or the extreme Application.CalculateFull (use sparingly, this would slow down a large worksheet).

AutoRefresh

run the RefreshAllDataConn sub every minute or whatever value you set on the line  Application.OnTime Now + TimeValue(“00:01:00”), “AutoRefresh”

Workbook_Open

an in-built Excel function that runs automatically when the worksheet is opened.  In this case it starts AutoRefresh.

PowerQuery data Connection workaround

The disadvantage of the VBA approach is that a .xlsm worksheet is necessary (macro enabled Excel worksheet).  There can be problems sharing macro enabled files because of security concerns.

The arrival of PowerQuery / Get and Transform means there’s another way to force a worksheet recalculation.  It’s a workaround and not perfect, but it’s possible and doesn’t need a macro-enabled worksheet.

In short, ensure that there’s a data query setup with auto-refresh.  If there isn’t a data connection, add a small one to the worksheet.

Once you have an auto-refreshing query, the worksheet including any volatile functions should also refresh.

Ideally the Stock and Geo data types should also refresh.

Any Excel data query comes with some refresh options in the Query properties.  Most of them default OFF.

Refresh every nnn minutes –  defaults off with 60 minutes suggested.

Refresh data when opening the file

Enable background refresh

Refresh this connection on Refresh All

The auto-refresh workaround is to create a small and practically insignificant data connection.  Then configure that data connection to update every minute or whatever time you wish.  That should force the worksheet to update including the volatile functions mentioned above.

Some versions of this workaround add a link to a tiny csv file on the same computer.  We’ve got the same result using a data connection from a table in the worksheet.

Create a small table with a single cell.  The cell can have anything but we create a cell with NOW() in it, for reasons we’ll explain later.

Select the table then choose Data | Get Data | From other sources | from Table/Range.  The exact menu item maybe different depending on your version of Excel.

When the Query Editor opens, just Close and Load it.  In the data connections pane, you’ll see a query.

Right-click the query, choose properties to see the settings we need.

Set the refresh rate that suits you.

To be tidy, we move the source table (right) onto the same sheet at the loaded query (left).  Because we used NOW() in the source table cell, it’ll be easy to see when/if the worksheet has refreshed.

VBA Code example Sub RefreshAllDataConn()     ' Refresh all Data Connections.     ' This should include Stock and Currency data types, even though they aren't listed.     Workbooks(ThisWorkbook.Name).RefreshAll     ' Show update time on status bar to confirm.     ' comment these lines out if not needed.     Application.DisplayStatusBar = True     Application.StatusBar = "Refreshed at: " & Now() End Sub Sub AutoRefresh() ' to run a Refresh All on the workbook every n minutes    RefreshAllDataConn    ' Repeat every minute or change to whatever value you prefer.    Application.OnTime Now + TimeValue("00:01:00"), "AutoRefresh" ' this is a simple example. There's no coded way to exit this function. End Sub Private Sub Workbook_Open() ' Starts the automatic refresh when the workbook is opened, ' commented out as a precaution.     ' AutoRefresh End Sub

Excel’s Stock / Currency data (kinda, sorta) gets auto-refreshMake automatic Excel worksheet list or table of contents

Complete Excel NetworkDays() solution with holidays & vacations

Thanks for subscribing!


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3